package com.api.signature.auto.interceptor;

import com.api.signature.auto.strategy.ApiSignatureStrategy;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import java.lang.reflect.Method;

/**
 * @author Jerry
 * @date 2024-01-24 10:41
 * api签名注解拦截器
 */
@Component
public class AnnotationInterceptor implements HandlerInterceptor {


    /**
     * 每次请求之前触发的方法
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        // 获取处理method
        if (!(handler instanceof HandlerMethod)) {
            return true;
        }
        Method method = ((HandlerMethod) handler).getMethod();
        // 进行验证
        ApiSignatureStrategy.INSTANCE.checkMethodAnnotation.accept(method, request);
        // 通过验证
        return true;
    }
}
